<?php
/*
	本插件：$this->model('model')->方法名();
	其他地方调用：model('jy_getred/model')->方法名();
*/
namespace jy_getred;
defined('BY_JYA') or exit('error');
class red extends \plugin{
	public function gored(){
		$red['set']=$this->plugin_setting('basic');
		if(!$red['set']['is_open']){
			return $red;
		}
		$daytime = mktime(0,0,0,date('m'),date('d'),date('Y'));
		$day=date('Y-m-d');
		$timeids=$red['set']['timeids'];
		if($timeids){
			$timeids=str_array($timeids);
			$timelist=array();
			foreach($timeids as $key=>$hour){
				$startime=strtotime($day.' '.$hour);
				$timelist[]=array(
					'hour'=>$hour,
					'startime'=>$startime,
				);
			}
			paixu($timelist,'startime','asc');
			$timelist=array_values($timelist);
			$issendnum=0;
			foreach($timelist as $key=>&$item){
				$nkey=$key+1;
				if($timelist[$nkey]['startime']){
					$item['endtime']=$timelist[$nkey]['startime'];
				}else{
					$item['endtime']=strtotime($day.' 23:59');
				}
				if($item['startime']<TIMESTAMP && $item['endtime']>TIMESTAMP){
					$item['status']=1;
					$issendnum++;
					$nowsendlog=$item;
				}elseif($item['startime']>TIMESTAMP && $item['endtime']>TIMESTAMP){
					$item['status']=0;
				}elseif($item['endtime']<TIMESTAMP){
					$issendnum++;
					$item['status']=2;
				}
			}
		}
		if($nowsendlog){
			$lastlog=pdo_get('jy_getred_sendredlog',array('createtime between'=>array($nowsendlog['startime'],$nowsendlog['endtime']),'uniacid'=>$_SESSION['uniacid']),'','createtime desc');
		}
		if($nowsendlog){
			if(!$lastlog['id']){
				$lastlog=$this->addsendredlog($red['set']);
			}
		}else{
			pdo_update('jy_getred_sendredlog',array('status'=>1),array('status'=>0,'uniacid'=>$_SESSION['uniacid']));
		}
		$pindex = max(1, intval($_RQ['page']));
		$psize = 10;
		$total='';
		$params=array('uniacid'=>$_SESSION['uniacid'],'isjian'=>0);
		$logs=pdo_getslice('jy_getred_redlog',$params,array($pindex,$psize),$total,$fields,'','id desc');
		$tips='';
		foreach($logs as $lv){
			$nickname=pdo_getcolumn('core_members',array('id'=>$lv['mid'],'uniacid'=>$_SESSION['uniacid']), 'nickname');
			$time=date('Y-m-d H:i:s',$lv['createtime']);
			$reward_name=$this->jiangtype[$lv['type']];
			$tips .=$nickname.'[抽到《'.$reward_name.'》,时间:'.$time.']；';
		}
		$red['tips']=$tips;
		$red['issendnum']=$issendnum;
		$red['getrednum']=pdo_count('jy_getred_redlog',array('mid'=>$_SESSION['uid'],'type'=>2,'uniacid'=>$_SESSION['uniacid']));
		$red['getallfee']=pdo_getsum('jy_getred_redlog',array('redid'=>$lastlog['id'],'uniacid'=>$_SESSION['uniacid'],'type'=>2),'fee');
		$red['mygetallfee']=pdo_getsum('jy_getred_redlog',array('mid'=>$_SESSION['uid'],'type'=>2,'uniacid'=>$_SESSION['uniacid']),'fee');
		$red['lastfee']=price($lastlog['allfee']-$red['getallfee'],'int');
		$lastlog['allfee']=price($lastlog['allfee']);
		$red['sendred']=$lastlog;
		$red['timelist']=$timelist;
		$red['nowsendlog']=$nowsendlog?:array();
		return $red;
	}
	public function addsendredlog($info=array()){
		$allfee=0;
		$total=0;
		foreach($info['feeset'] as $iv){
			$allfee +=$iv['fee']*$iv['num'];
			$total +=$iv['num'];
		}
		$lastlog=array(
					'uniacid'=>$_SESSION['uniacid'],
					'info'=>ijson_encode($info),
					'allfee'=>$allfee,
					'total'=>$total,
					'status'=>0,
					'createtime'=>TIMESTAMP
		);
		pdo_insert('jy_getred_sendredlog',$lastlog);
		$lastlog['id']=pdo_insertid();
		return $lastlog;
	}
	public function getred($uid=0,$sendredlogid=0){
			$uid=$uid?:$_SESSION['uid'];
			$daytime = mktime(0,0,0,date('m'),date('d'),date('Y'));
			$redset=$this->plugin_setting('basic');
			$canyu=$redset['canyu']?str_array($redset['canyu']):'';
			$iscanyu=0;
			$msg='';
			foreach($canyu as $ctype){
				if($ctype){
					$issetcanyu=1;
				}
				if($ctype==1){
					$iscanyu=pdo_count('jy_weishop_order',array('createtime >='=>$daytime,'ispay'=>1,'mid'=>$uid,'uniacid'=>$_SESSION['uniacid']));
					$msg.='今日下单一笔即可参与';
				}elseif($ctype==2){
					$iscanyu=pdo_count('jy_weishop_member',array('createtime >='=>$daytime,'merchid'=>0,'mid'=>$uid,'uniacid'=>$_SESSION['uniacid']));
				}elseif($ctype==3){
					$order=pdo_get('jy_weishop_order',array('ispay'=>1,'mid'=>$uid,'uniacid'=>$_SESSION['uniacid']),array('id','createtime'),'createtime desc');
					if($order['createtime'] && $order['createtime']<TIMESTAMP-30*24*3600){
						$iscanyu=1;
					}
					if($msg){
						$msg.=',或最近购物时间满1月可参与';
					}else{
						$msg.='最近购物时间满1月可参与';
					}
				}elseif($ctype==4){
					$order=pdo_get('jy_weishop_order',array('ispay'=>1,'mid'=>$uid,'uniacid'=>$_SESSION['uniacid']),array('id','createtime'),'createtime desc');
					if($order['createtime'] && $order['createtime']<TIMESTAMP-15*24*3600){
						$iscanyu=1;
					}
					if($msg){
						$msg.=',或最近购物时间满15天可参与';
					}else{
						$msg.='最近购物时间满15天可参与';
					}
				}
				if($iscanyu){
					break;
				}
			}
			if($issetcanyu && !$iscanyu){
				exi('很遗憾！您当前不能参与该活动！'.$msg,'error');
			}
			$sendlog=pdo_get('jy_getred_sendredlog',array('id'=>$sendredlogid,'uniacid'=>$_SESSION['uniacid']));
			$info=json_decode($sendlog['info'],true);
			$jiangset=$info['jiangset'];
			$feeset=$info['feeset'];
			$jifenset=$info['jifenset'];
			$isget=pdo_count('jy_getred_redlog',array('createtime >='=>$daytime,'mid'=>$uid,'uniacid'=>$_SESSION['uniacid']));
			if($isget>=$redset['cannum']){
				exi('很遗憾！没有抽中红包！','error');
			}
			$jiangs = array();
			foreach($jiangset as $key =>$value){
				$jiangs[$key] = $value['rate'];
			}
			$jiangkey = $this->getRand($jiangs);
			$jiangitem=$jiangset[$jiangkey];
			if($jiangitem['type']==1){
				$temreward = array();
				foreach($jifenset as $key =>$value){
					if(!$value['isnum'] || $value['num']>$value['isnum']){
						$temreward[$key] = $value['num'];
					}
				}
				if(count($temreward)<=0){
					exi('很遗憾！积分红包已抽完！','error');
				}
				$infokey = $this->getRand($temreward);
				$fee=$jifenset[$infokey]['fee'];
				$this->addredlog($sendlog,$fee,$infokey,$uid,1);
				$jiangset[$infokey]['isnum'] +=1;
				$info['jiangset']=$jiangset;
				$jiangitem['fee']=$fee;
				pdo_update('jy_getred_sendredlog',array('info'=>ijson_encode($info)),array('id'=>$sendredlogid,'uniacid'=>$_SESSION['uniacid']));
				$this->upmyfee($sendlog,$uid,$fee,1,0);
			}elseif($jiangitem['type']==2){
				$temreward = array();
				foreach($feeset as $key =>$value){
					if(!$value['isnum'] || $value['num']>$value['isnum']){
						$temreward[$key] = $value['num'];
					}
				}
				if(count($temreward)<=0){
					exi('很遗憾！消费金红包已抽完！','error');
				}
				$infokey = $this->getRand($temreward);
				$fee=$feeset[$infokey]['fee'];
				$this->addredlog($sendlog,$fee,$infokey,$uid,2);
				$feeset[$infokey]['isnum'] +=1;
				$info['feeset']=$feeset;
				$jiangitem['fee']=$fee;
				pdo_update('jy_getred_sendredlog',array('info'=>ijson_encode($info)),array('id'=>$sendredlogid,'uniacid'=>$_SESSION['uniacid']));
				$this->upmyfee($sendlog,$uid,$fee,2,0);
			}elseif($jiangitem['type']==3 || $jiangitem['type']==4){
				if($jiangitem['couponid']){
					model('jy_weishop/coupon')->getcoupon($jiangitem['couponid'],$_SESSION['uid'],1);
				}
			}else{
				exi('奖品不存在，请联系客服完善奖品设置！','error');
			}
			return $jiangitem;
	}
	public function addredlog($sendlog=array(),$fee='',$infokey='',$uid='',$type=0){
		$uid=$uid?:$_SESSION['uid'];
		$redlog=array(
					'uniacid'=>$_SESSION['uniacid'],
					'mid'=>$uid,
					'redid'=>$sendlog['id'],
					'fee'=>$fee,
					'infokey'=>$infokey,
					'type'=>$type,
					'createtime'=>TIMESTAMP
		);
		pdo_insert('jy_getred_redlog',$redlog);
		return $true;
	}
	private function getRand($proArr){
		$result = '';
		$proSum = array_sum($proArr);
		foreach ($proArr as $key => $proCur) {
			$randNum = mt_rand(1, $proSum);
			if ($randNum <= $proCur) {
				$result = $key;
				break;
			}
			$proSum -= $proCur;
		}
		unset($proArr);
		return intval($result);
	}
}
?>